<template>
  <div class="news-detail">
    <div class="detail-container" v-loading="loading">
      <!-- 文章头部 -->
      <div class="article-header">
        <h1 class="article-title">{{ newsDetail.title }}</h1>
        <div class="article-meta">
          <div class="meta-left">
            <el-tag :type="getCategoryType(newsDetail.category)" size="small">
              {{ newsDetail.category }}
            </el-tag>
            <span class="publish-time">
              <el-icon><Calendar /></el-icon>
              {{ formatTime(newsDetail.createTime) }}
            </span>
            <span class="views">
              <el-icon><View /></el-icon>
              {{ newsDetail.views }} 阅读
            </span>
          </div>
          <div class="meta-right">
            <el-button text @click="handleShare">
              <el-icon><Share /></el-icon>
              分享
            </el-button>
          </div>
        </div>
      </div>

      <!-- 文章内容 -->
      <div class="article-content">
        <div v-if="newsDetail.content" v-html="newsDetail.content"></div>
        <div v-else class="empty-content">
          <el-empty description="暂无内容" />
        </div>
      </div>

      <!-- 文章底部 -->
      <div class="article-footer">
        <div class="tags" v-if="newsDetail.tags && newsDetail.tags.length">
          <el-icon><Collection /></el-icon>
          <el-tag
            v-for="tag in newsDetail.tags"
            :key="tag"
            size="small"
            effect="plain"
            class="tag-item"
          >
            {{ tag }}
          </el-tag>
        </div>
        
        <div class="navigation">
          <div class="prev" v-if="adjacentNews.prev">
            <span class="label">上一篇：</span>
            <router-link :to="{ name: 'NewsDetail', params: { id: adjacentNews.prev.id }}">
              {{ adjacentNews.prev.title }}
            </router-link>
          </div>
          <div class="next" v-if="adjacentNews.next">
            <span class="label">下一篇：</span>
            <router-link :to="{ name: 'NewsDetail', params: { id: adjacentNews.next.id }}">
              {{ adjacentNews.next.title }}
            </router-link>
          </div>
          <div v-if="!adjacentNews.prev && !adjacentNews.next" class="no-adjacent">
            暂无更多文章
          </div>
        </div>
      </div>

      <!-- 返回按钮 -->
      <div class="actions">
        <el-button type="primary" @click="goBack">返回列表</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Calendar, View, Share, Collection } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { getNewsDetail, getAdjacentNews } from '@/api/news'

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const newsDetail = ref({})
const adjacentNews = ref({
  prev: null,
  next: null
})

// 获取分类标签类型
const getCategoryType = (category) => {
  const types = {
    '公司新闻': 'primary',
    '行业动态': 'success',
    '技术分享': 'warning',
    '通知公告': 'info'
  }
  return types[category] || 'info'
}

// 格式化时间
const formatTime = (date) => {
  if (!date) return ''
  return new Date(date).toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  })
}

// 获取新闻详情
const fetchNewsDetail = async () => {
  loading.value = true
  try {
    const [detailRes, adjacentRes] = await Promise.all([
      getNewsDetail(route.params.id),
      getAdjacentNews(route.params.id)
    ])
    newsDetail.value = detailRes.data
    adjacentNews.value = adjacentRes.data
  } catch (error) {
    console.error('获取新闻详情失败:', error)
    ElMessage.error('获取新闻详情失败')
  } finally {
    loading.value = false
  }
}

// 分享功能
const handleShare = () => {
  // 实现分享功能
  ElMessage.success('分享功能开发中')
}

// 返回列表
const goBack = () => {
  router.push('/news')
}

// 监听路由参数变化，重新获取数据
watch(() => route.params.id, (newId) => {
  if (newId) {
    fetchNewsDetail()
  }
})

onMounted(() => {
  fetchNewsDetail()
})
</script>

<style lang="scss" scoped>
.news-detail {
  padding: 60px 0;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  min-height: 100vh;
  
  .detail-container {
    max-width: 1000px;
    margin: 0 auto;
    padding: 50px;
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.8);
  }
  
  .article-header {
    margin-bottom: 50px;
    text-align: left;
    
    .article-title {
      font-size: 36px;
      font-weight: 700;
      background: linear-gradient(45deg, #2c3e50 30%, #3498db 100%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      margin-bottom: 25px;
      line-height: 1.4;
      letter-spacing: -0.5px;
    }
    
    .article-meta {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 15px 0;
      border-top: 1px solid rgba(52, 152, 219, 0.2);
      border-bottom: 1px solid rgba(52, 152, 219, 0.2);
      background: linear-gradient(to right, rgba(52, 152, 219, 0.05), transparent, rgba(52, 152, 219, 0.05));
      
      .meta-left {
        display: flex;
        align-items: center;
        gap: 25px;
        
        .el-tag {
          font-weight: 500;
          padding: 6px 12px;
          border-radius: 20px;
          background: linear-gradient(45deg, var(--el-color-primary) 0%, var(--el-color-primary-light-3) 100%);
          border: none;
          color: white;
          box-shadow: 0 2px 8px rgba(var(--el-color-primary-rgb), 0.2);
        }
        
        .publish-time,
        .views {
          display: flex;
          align-items: center;
          gap: 6px;
          color: #5c6f88;
          font-size: 15px;
          
          .el-icon {
            font-size: 18px;
            color: #3498db;
          }
        }
      }
      
      .meta-right {
        :deep(.el-button) {
          color: #5c6f88;
          font-size: 15px;
          
          .el-icon {
            font-size: 18px;
            margin-right: 4px;
            color: #3498db;
          }
          
          &:hover {
            color: #3498db;
            background: rgba(52, 152, 219, 0.1);
          }
        }
      }
    }
  }
  
  .article-content {
    margin-bottom: 60px;
    line-height: 1.8;
    color: #2c3e50;
    font-size: 16px;
    
    :deep(h1, h2, h3, h4, h5, h6) {
      margin: 2em 0 1em;
      font-weight: 600;
      line-height: 1.4;
      background: linear-gradient(45deg, #2c3e50 30%, #3498db 100%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
    
    :deep(h1) { font-size: 32px; }
    :deep(h2) { font-size: 28px; }
    :deep(h3) { font-size: 24px; }
    :deep(h4) { font-size: 20px; }
    :deep(h5) { font-size: 18px; }
    :deep(h6) { font-size: 16px; }
    
    :deep(p) {
      margin: 1.5em 0;
      color: #34495e;
      line-height: 1.8;
    }
    
    :deep(img) {
      max-width: 100%;
      height: auto;
      border-radius: 12px;
      margin: 2em auto;
      display: block;
      box-shadow: 0 5px 15px rgba(52, 152, 219, 0.15);
      border: 1px solid rgba(52, 152, 219, 0.1);
    }
    
    :deep(a) {
      color: #3498db;
      text-decoration: none;
      background: linear-gradient(to bottom, transparent 0%, transparent 60%, rgba(52, 152, 219, 0.2) 60%, rgba(52, 152, 219, 0.2) 100%);
      transition: all 0.3s ease;
      
      &:hover {
        color: #2980b9;
        background: linear-gradient(to bottom, transparent 0%, transparent 60%, rgba(41, 128, 185, 0.4) 60%, rgba(41, 128, 185, 0.4) 100%);
      }
    }
    
    :deep(blockquote) {
      margin: 2em 0;
      padding: 1.5em 2em;
      border-left: 4px solid #3498db;
      background: linear-gradient(to right, rgba(52, 152, 219, 0.1), rgba(52, 152, 219, 0.05));
      color: #34495e;
      font-style: italic;
      border-radius: 0 8px 8px 0;
      box-shadow: 0 2px 8px rgba(52, 152, 219, 0.1);
    }
    
    :deep(code) {
      background: linear-gradient(45deg, rgba(52, 152, 219, 0.1), rgba(52, 152, 219, 0.05));
      padding: 0.2em 0.4em;
      border-radius: 4px;
      font-family: 'Fira Code', monospace;
      font-size: 0.9em;
      color: #2980b9;
      border: 1px solid rgba(52, 152, 219, 0.2);
    }
    
    :deep(ul, ol) {
      margin: 1.5em 0;
      padding-left: 2em;
      
      li {
        margin: 0.5em 0;
        color: #34495e;
        
        &::marker {
          color: #3498db;
        }
      }
    }
    
    :deep(table) {
      width: 100%;
      margin: 2em 0;
      border-collapse: separate;
      border-spacing: 0;
      border-radius: 8px;
      overflow: hidden;
      box-shadow: 0 2px 8px rgba(52, 152, 219, 0.1);
      
      th, td {
        padding: 12px;
        border: 1px solid rgba(52, 152, 219, 0.2);
      }
      
      th {
        background: linear-gradient(45deg, rgba(52, 152, 219, 0.1), rgba(52, 152, 219, 0.05));
        color: #2c3e50;
        font-weight: 600;
      }
      
      tr:hover {
        background: rgba(52, 152, 219, 0.05);
      }
    }
  }
  
  .article-footer {
    margin-bottom: 50px;
    padding-top: 40px;
    border-top: 1px solid rgba(52, 152, 219, 0.2);
    
    .tags {
      display: flex;
      align-items: center;
      gap: 12px;
      margin-bottom: 35px;
      
      .el-icon {
        color: #3498db;
        font-size: 20px;
      }
      
      .tag-item {
        padding: 6px 15px;
        border-radius: 20px;
        font-size: 14px;
        border: none;
        background: linear-gradient(45deg, rgba(52, 152, 219, 0.1), rgba(52, 152, 219, 0.05));
        color: #3498db;
        box-shadow: 0 2px 8px rgba(52, 152, 219, 0.1);
        
        &:hover {
          background: linear-gradient(45deg, rgba(52, 152, 219, 0.15), rgba(52, 152, 219, 0.1));
          transform: translateY(-1px);
        }
      }
    }
    
    .navigation {
      display: flex;
      flex-direction: column;
      gap: 20px;
      background: linear-gradient(45deg, rgba(52, 152, 219, 0.1), rgba(52, 152, 219, 0.05));
      padding: 25px;
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(52, 152, 219, 0.1);
      border: 1px solid rgba(52, 152, 219, 0.2);
      
      .prev,
      .next {
        display: flex;
        align-items: center;
        gap: 12px;
        
        .label {
          color: #5c6f88;
          font-weight: 500;
          min-width: 70px;
        }
        
        a {
          color: #34495e;
          text-decoration: none;
          font-size: 15px;
          transition: all 0.3s ease;
          padding: 4px 0;
          background: linear-gradient(to bottom, transparent 0%, transparent 60%, rgba(52, 152, 219, 0.2) 60%, rgba(52, 152, 219, 0.2) 100%);
          
          &:hover {
            color: #3498db;
            background: linear-gradient(to bottom, transparent 0%, transparent 60%, rgba(52, 152, 219, 0.4) 60%, rgba(52, 152, 219, 0.4) 100%);
          }
        }
      }

      .no-adjacent {
        text-align: center;
        color: #5c6f88;
        font-size: 15px;
        padding: 10px 0;
      }
    }
  }
  
  .actions {
    text-align: center;
    
    :deep(.el-button) {
      padding: 12px 35px;
      font-size: 16px;
      font-weight: 500;
      border-radius: 25px;
      background: linear-gradient(45deg, #3498db 0%, #2980b9 100%);
      border: none;
      transition: all 0.3s ease;
      box-shadow: 0 4px 15px rgba(52, 152, 219, 0.3);
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(52, 152, 219, 0.4);
        background: linear-gradient(45deg, #2980b9 0%, #3498db 100%);
      }
    }
  }
  
  @media (max-width: 768px) {
    padding: 30px 0;
    
    .detail-container {
      padding: 30px 20px;
      margin: 0 15px;
    }
    
    .article-header {
      margin-bottom: 30px;
      
      .article-title {
        font-size: 26px;
        margin-bottom: 20px;
      }
      
      .article-meta {
        flex-direction: column;
        gap: 15px;
        padding: 15px 0;
        
        .meta-left {
          flex-wrap: wrap;
          justify-content: flex-start;
          gap: 15px;
        }
      }
    }
    
    .article-content {
      margin-bottom: 40px;
      font-size: 15px;
      
      :deep(h1) { font-size: 24px; }
      :deep(h2) { font-size: 22px; }
      :deep(h3) { font-size: 20px; }
      :deep(h4) { font-size: 18px; }
      :deep(h5) { font-size: 16px; }
      :deep(h6) { font-size: 15px; }
      
      :deep(blockquote) {
        padding: 1em 1.5em;
      }
    }
    
    .article-footer {
      margin-bottom: 30px;
      padding-top: 30px;
      
      .tags {
        flex-wrap: wrap;
        margin-bottom: 25px;
      }
      
      .navigation {
        padding: 20px;
      }
    }
  }
}

.empty-content {
  padding: 60px 0;
  text-align: center;
  background: #f8f9fa;
  border-radius: 8px;
  color: #909399;
}
</style> 